<template>
  <div class="setting-container">
    <el-card class="card">
      <el-tabs v-model="activeName">
        <el-tab-pane label="角色管理" name="first">
          <el-col>
            <el-button type="primary" size="mini" @click="add"
              >+新增角色</el-button
            >
          </el-col>
          <el-table :data="tableData" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="180">
            </el-table-column>
            <el-table-column prop="name" label="角色名" width="180">
            </el-table-column>
            <el-table-column prop="description" label="描述"> </el-table-column>
            <el-table-column label="操作" width="230">
              <template slot-scope="scope">
                <el-button type="text" @click="showSetRole(scope.row)">分配权限</el-button>
                <el-button type="text" @click="update(scope.row)"
                  >修改</el-button
                >
                <el-button type="text" @click="delRole(scope.row)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            style="text-align:right"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="pagesizes"
            :page-size="pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          >
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="公司信息" name="second">
          <el-form :model="companyInfo" class="myform" label-width="100px">
            <el-form-item label="企业名称">
              <el-input disabled v-model="companyInfo.name"></el-input>
            </el-form-item>
            <el-form-item label="公司地址">
              <el-input
                disabled
                v-model="companyInfo.companyAddress"
              ></el-input>
            </el-form-item>
            <el-form-item label="公司电话">
              <el-input disabled v-model="companyInfo.companyPhone"></el-input>
            </el-form-item>
            <el-form-item label="邮箱">
              <el-input disabled v-model="companyInfo.mailbox"></el-input>
            </el-form-item>
            <el-form-item label="备注">
              <el-input disabled v-model="companyInfo.remarks"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-dialog
        :title="form.id ? '编辑角色' : '新增角色'"
        :visible.sync="dialogFormVisible"
        @close="cancle">
        <el-form :model="form">
          <el-form-item label="角色名称" label-width="100px">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="角色描述" label-width="100px">
            <el-input v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="cancle">取 消</el-button>
          <el-button type="primary" @click="submit">确 定</el-button>
        </div>
      </el-dialog>
      <setrole :showRole.sync='showRole' :myname='myname' ref="myrole"></setrole>
    </el-card>
  </div>
</template>

<script>
import {
  getCompanyApi,
  getRoleApi,
  updateRoleApi,
  addRoleApi,
  delRoleApi,
  getCPApi
} from '@/api/setting'
export default {
  name: 'setting',
  data () {
    return {
      activeName: 'first',
      tableData: [],
      currentPage: 1,
      total: 1,
      pagesizes: [1, 2, 3, 4],
      pagesize: 4,
      dialogFormVisible: false,
      form: {
        name: '',
        description: ''
      },
      companyInfo:{

      },
      //子组件的显示与隐藏
      showRole:false,
      myname:''
    }
  },
  created () {
    this.getCompanyInfo()
    this.getComoanyInfo()
  },
  components:{
    setrole:()=>import('./components/setrole.vue')
  },
  methods: {
    //获取企业信息
    async getCompanyInfo () {
      let res = await getCompanyApi({
        page: this.currentPage,
        pagesize: this.pagesize
      })
      console.log(res)
      this.tableData = res.data.rows
      this.total = res.data.total
    },
    //处理页码大小
    handleSizeChange (val) {
      console.log(val)
      this.pagesize = val
      this.getCompanyInfo()
    },
    //处理当前页变动
    handleCurrentChange (val) {
      console.log(val)
      this.currentPage = val
      this.getCompanyInfo()
    },
    //新增角色
    async add () {
      this.dialogFormVisible = true
    },
    //点击编辑角色
    async update (row) {
      this.dialogFormVisible = true
      // console.log(row);
      let res = await getRoleApi(row.id)
      console.log(res)
      this.form = { ...row }
    },
    //删除角色
    delRole (row) {
      this.$confirm('您确定要删除吗', '提示')
        .then(async () => {
          let res = await delRoleApi(row.id)
          console.log(res)
          if (this.tableData.length == 1) {
            this.currentPage--
          }
          this.$message.success('删除成功')
          this.getCompanyInfo()
        })
        .catch()
    },
    //确认角色
    async submit () {
      if (this.form.id) {
        var res = await updateRoleApi(this.form)
        console.log(res)
      } else {
        //新增
        var res = await addRoleApi(this.form)
        console.log(res)
      }
      this.cancle()
      this.$message.success(res.message)
      this.getCompanyInfo()
    },
    //取消弹出框
    cancle () {
      this.dialogFormVisible = false
      this.form = {
        name: '',
        description: ''
      }
    },
    //查询公司信息
    async getComoanyInfo(){
      const companyId = this.$store.getters.userInfo.companyId
      let res = await getCPApi(companyId)
      console.log(res);
      this.companyInfo = res.data
    },
    //打开分配权限的面板
    showSetRole(row){
      this.showRole = true
      // console.log(row);
      this.myname = row.name
      this.$refs.myrole.getRolesId(row.id)
    }
  }
}
</script>

<style lang="scss">
.setting-container {
  .card {
    padding: 40px;
  }
}
</style>
